package com.struts2.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.beans.CommonUserBean;
import com.beans.Job;
import com.dbms.DatabaseManageSystem;
import com.dbms.DatabaseParameterList;
import com.dbms.Pages;

public class UserAction {
	DatabaseManageSystem dbms=new DatabaseManageSystem();
	private Pages pager = new Pages();
	
	private int userNo;
	private List<Job> jobs;
	
	private CommonUserBean user;
	private List<CommonUserBean> list;
	//添加用户
	public String addUser() throws IOException{
		int user_no=dbms.getNumber("select seq_userinfo.nextval from dual", false, false);
		//
		int count_result=0;
		int count=0;
		for(Job job:jobs){
			if(job.getJob_company()!=null && job.getJob_start_date()!=null && job.getJob_end_date()!=null){
				count++;
				DatabaseParameterList js=new DatabaseParameterList();
				js.add("JOB_NO", "seq_job.nextval",true);
				job.setUser_no(user_no);
				boolean js_result = dbms.insertObject("job", job, js, true,false, false);
				if(js_result)
					count_result++;
			}
		}
		boolean result=false;
		if(count_result==count){
			user.setUser_no(user_no);
			user.setRegiste_date(new Date());
			result = dbms.insertObject("userinfo",user, null, false, false,false);
		}
		if(result){
			dbms.commit();//提交事务
			dbms.closeAll();
			return "list";
		}else{
			dbms.rollback();//事务回滚
			dbms.closeAll();
			return "input";
		}
	}
	//用户列表
	public String listUser(){
		list = (List<CommonUserBean>) dbms.getList("select user_no,sex,user_id,name,registe_date,birthday from userinfo",pager, CommonUserBean.class, false, true, true);
		return "list";
	}
	//进入修改用户页
	public String modifyUser(){
		StringBuilder sql=new StringBuilder();
		sql.append("select ");
		sql.append("user_id,");
		sql.append("user_password,");
		sql.append("name,");
		sql.append("registe_date,");
		sql.append("birthday,");
		sql.append("sex");
		sql.append(" from userinfo");
		sql.append(" where");
		sql.append(" user_no=").append(userNo);
		user=new CommonUserBean();
		dbms.getToObject(sql.toString(), user, false, true, false);
		jobs=(List<Job>) dbms.getList("select * from job where user_no="+userNo, Job.class, false, false, false);
		dbms.closeAll();
		return "modify";
	}
	//修改用户
	public String updateUser() throws IOException{
		DatabaseParameterList body=new DatabaseParameterList();
		body.add("registe_date", "sysdate",true);
		for(Job job:jobs){
			if(job.getJob_no()!=null && job.getJob_company()!=null && job.getJob_start_date()!=null && job.getJob_end_date()!=null){
				dbms.updateObject("JOB", job, null, "JOB_NO="+job.getJob_no(), true, true, false);
			}
		}
		boolean result=dbms.updateObject("userinfo",user, body,new StringBuilder("user_no=").append(userNo).toString(), false,true, true);
		if(result)
			return "view";
		else
			return "modify";
	}
	//查看用户
	public String viewUser(){
		StringBuilder sql=new StringBuilder();
		sql.append("select ");
		sql.append("user_id,");
		sql.append("user_password,");
		sql.append("name,");
		sql.append("registe_date,");
		sql.append("birthday,");
		sql.append("image as image_b,");
		sql.append("sex");
		sql.append(" from userinfo");
		sql.append(" where");
		sql.append(" user_no=").append(userNo);
		CommonUserBean bean=(CommonUserBean) dbms.getObject(sql.toString(), CommonUserBean.class, false, true, true);
		HttpServletRequest request=ServletActionContext.getRequest();
		request.setAttribute("userInfo", bean);
		return "view";
	}
	//查看图片
	public void viewImage() throws IOException{
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("image/jpeg;");
		OutputStream os=response.getOutputStream();
		StringBuilder sql=new StringBuilder();
		sql.append("select image as image_b from userinfo");
		sql.append(" where");
		sql.append(" user_no=").append(userNo);
		CommonUserBean bean=(CommonUserBean) dbms.getObject(sql.toString(), CommonUserBean.class, false, true, true);
		os.write(bean.getImage_b());
		os.flush();
		os.close();
	}
	
	public List<CommonUserBean> getList() {
		return list;
	}
	public List<Job> getJobs() {
		return jobs;
	}
	public void setJobs(List<Job> jobs) {
		this.jobs = jobs;
	}
	public Pages getPager() {
		return pager;
	}
	public CommonUserBean getUser() {
		return user;
	}
	public void setUser(CommonUserBean user) {
		this.user = user;
	}
	public int getUserNo() {
		return userNo;
	}
	public void setUserNo(int userNo) {
		this.userNo = userNo;
	}
}
